package org.business.accounting.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.business.accounting.interfaces.BasicOrder;
import org.business.accounting.interfaces.Cart;
import org.business.accounting.interfaces.ComputerOrder;

public class CartBean implements Cart {
	
	private List<BasicOrder> basicOrders;
	private double price;
	
	//
	// Deviding orders
	//
	
	private Collection<ComputerOrder> computerOrders;
	
	public CartBean() {
		basicOrders = new ArrayList<BasicOrder>();
	}
	
	//
	// Deviding Orders
	//
	
	public void divideOrders() {
		computerOrders = divideComputerOrders();
	}
	
	private Collection<ComputerOrder> divideComputerOrders() {
		Iterator<BasicOrder> basicOrderItr = basicOrders.iterator();		
		
		Collection<ComputerOrder> collection = new Vector<ComputerOrder>();
		BasicOrder basicOrder;
		while(basicOrderItr.hasNext()) {
			basicOrder = basicOrderItr.next();
			if(basicOrder instanceof ComputerOrder) {
				collection.add((ComputerOrder) basicOrder);
			}
		}
		return collection;
	}
	
	//
	//
	//
	
	public double calculatePrice() {
		double tempPrice = 0;
		Iterator<BasicOrder> iterator = basicOrders.iterator();
		while (iterator.hasNext()) {
			tempPrice += iterator.next().calculatePrice();
		}
		this.price = tempPrice;
		return price;
	}
	
	public boolean add(BasicOrder basicOrder) {
		return basicOrders.add(basicOrder);
	}
	
	public boolean remove(BasicOrder basicOrder) {
		return basicOrders.remove(basicOrder);
	}
	
	public void clear() {
		basicOrders.clear();
	}

	public List<BasicOrder> getBasicOrders() {
		return basicOrders;
	}

	public void setBasicOrders(List<BasicOrder> basicOrders) {
		this.basicOrders = basicOrders;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public Collection<ComputerOrder> getComputerOrders() {
		return computerOrders;
	}

	public void setComputerOrders(Collection<ComputerOrder> computerOrders) {
		this.computerOrders = computerOrders;
	}

}
